#define _CRT_SECURE_NO_WARNINGS 1
#include <assert.h>
#include <stdio.h>
void Swep(int* a, int* b)
{
	assert(a && b);

	int tmp = *a;
	*a = *b;
	*b = tmp;
}
void AdjustUp(int* a, int child)
{
	assert(a&&child);

	int father = (child - 1) / 2;
	while (child > 0)
	{
		if (a[father] < a[child])
		{
			Swep(&a[father], &a[child]);
		}
		else
		{
			break;
		}

		child = father;
		father = (child - 1) / 2;
	}
}

void AdjustDown(int* a, int n, int father)
{
	assert(a);

	int child = father * 2 + 1;
	
	while (child < n)
	{
		if ((child + 1 < n) && (a[child] < a[child + 1]))
		{
			child++;
		}
		if (a[child] > a[father])
		{
			Swep(&a[child], &a[father]);
		}
		else
		{
			break;
		}
		father = child;
		child = child * 2 + 1;
	}
	
}
void HeapSort(int* a, int n)
{
	assert(a&&n);

	int child = n - 1;
	while (child > 0)
	{
		AdjustUp(a, child);
		child--;
	}

	int end = n - 1;
	while (end > 0)
	{
		Swep(&a[0], &a[end]);
		AdjustDown(a,end,0);
		end--;
	}
	
}

int main()
{
	int a[10] = { 10,1,8,-7,1,5,4,1113,2,1 };
	HeapSort(a, 10);
	return 0;
}